package gui;

import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Scott
 */
public class InstructorUI extends GUI implements ListSelectionListener {

    //initialize panels for instructors
    JPanel InstructorButtonPanel = new JPanel();
    JPanel rosterPanel = new JPanel();

    //initialize instructor buttons
    JButton viewRosterButton = new JButton();
    JButton addUserButton = new JButton();
    JButton removeUserButton = new JButton();
    JButton editUserButton = new JButton();

    //initialize roster components
     private JList list;
     private DefaultListModel listModel;

    public InstructorUI(JFrame mainFrame){
        this.mainFrame = mainFrame;
    }

    public void drawInstructorComponents(){

    //set panel properties

     InstructorButtonPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
     rosterPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
     rosterPanel.setLayout(new BoxLayout(rosterPanel, BoxLayout.Y_AXIS));

    //set button labels
     viewRosterButton.setText("View Roster");
     addUserButton.setText("Add User");
     removeUserButton.setText("Remove User");
     editUserButton.setText("Edit User");

    //initially disable buttons
     addUserButton.setEnabled(false);
     removeUserButton.setEnabled(false);
     editUserButton.setEnabled(false);

    //add buttons to the panel
     InstructorButtonPanel.add(viewRosterButton);
     InstructorButtonPanel.add(addUserButton);
     InstructorButtonPanel.add(removeUserButton);
     InstructorButtonPanel.add(editUserButton);

    //add panel to the frame

     drawRosterPane();

     mainFrame.getContentPane().add(InstructorButtonPanel, BorderLayout.NORTH);
     mainFrame.getContentPane().add(rosterPanel, BorderLayout.WEST);
     mainFrame.setBounds(200,100, 800, 600);



    }
    
    public void drawRosterPane(){

        listModel = new DefaultListModel();
        listModel.addElement("--Add New Student--");
        listModel.addElement("Skow, Scott");
        listModel.addElement("Lehman, Kyle");
        listModel.addElement("Harb, Samir");
        listModel.addElement("Gandy, Ryan");
        listModel.addElement("Huff, Earl");

        /*
        ArrayList<Student> studentList = Database.getStudents();
        for(int i = 0; i < studentList.size(); i++){
         listModel.addElement(studentList.get(i).toString());
         }

         *This wont be sorted.. should we sort?
         *Should Database sort it beforehand??
         */

        //Create the list and put it in a scroll pane.
        list = new JList(listModel);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        list.addListSelectionListener(this);
        list.setVisibleRowCount(25);

        JScrollPane listScrollPane = new JScrollPane(list);
        JLabel studentRosterLabel = new JLabel("Student Roster");
        rosterPanel.add(studentRosterLabel);
        rosterPanel.add(listScrollPane);



    }

     public void valueChanged(ListSelectionEvent e) {
        if (e.getValueIsAdjusting() == false) {

            if (list.getSelectedIndex() == -1) {
            //No Selection: enable add user

                removeUserButton.setEnabled(false);
                addUserButton.setEnabled(true);
                editUserButton.setEnabled(false);

            } else if(list.getSelectedIndex() == 0){
            //Selected new user: disable remove & edit
                //enable add user
                removeUserButton.setEnabled(false);
                editUserButton.setEnabled(false);
                addUserButton.setEnabled(true);
            }
            else {
            //Single selection: permit all operations.
                removeUserButton.setEnabled(true);
                editUserButton.setEnabled(true);
                addUserButton.setEnabled(false);
            }
        }
     }
}